package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.dto.CancelOrderRequest;
import com.cskaoyan.order.dto.CancelOrderResponse;
import com.cskaoyan.order.service.OrderCoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 类说明
 *
 * @author zhangfuqiang
 * @date 2022/5/20
 */
@RestController
public class ShoppingCancelOrderController {


    @Autowired
    OrderCoreService orderCoreService;

    //http://localhost:8103/shopping/cancelOrder
    //{"id":"20041912580013939"}
    @PostMapping("/shopping/cancelOrder")
    public ResponseData cancelOrder(@RequestBody Map<String, String> map, HttpServletRequest request) {
        // 获取Token中的用户信息
        Long uid = null;
        try {
            String user_info = request.getHeader("user_info");
            JSONObject jsonObject = JSON.parseObject(user_info);
            uid = Long.parseLong(jsonObject.get("uid").toString());
        } catch (Exception e) {
            return new ResponseUtil().setErrorMsg("用户未登录");
        }
        //获取订单id
        String id = map.get("orderId");
        CancelOrderRequest cancelOrderRequest = new CancelOrderRequest();
        cancelOrderRequest.setOrderId(id);
        CancelOrderResponse response = orderCoreService.cancelOrder(cancelOrderRequest);
        if (OrderRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData("成功");
        }
        // 业务方法执行失败
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

}
